<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="24" v-if="method != 'add'">
            <el-form-item label="项目ID" prop="name"
                :rules="[
                 ]">
              <div>{{inputForm.id}}</div>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="项目名称" prop="name"
                :rules="[
                  {required: true, message:'项目名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写项目名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="项目类型" prop="name"
                :rules="[
                  {required: true, message:'项目类型不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.type" placeholder="请填写项目类型"     ></el-input>
           </el-form-item>
        </el-col>

        <el-col :span="12">
            <el-form-item label="楼层自带高度" prop="floorHasHeight"
                :rules="[
                  {required: true, message:'楼层自带高度不能为空', trigger:'blur'}
                 ]">
                    <el-radio-group v-model="inputForm.floorHasHeight">
                        <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
                    </el-radio-group>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                :rules="[
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="定位技术" prop="locationType"
            :rules="[
              {required: true, message:'定位技术不能为空', trigger:'blur'}
             ]">
            <el-checkbox-group
                v-model="locationTypeList">
               <el-checkbox v-for="item in $dictUtils.getDictList('location_type')" :label="item.label" :key="item.id">{{item.label}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="地图链接" prop="mapH5"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mapH5" placeholder="请填写地图链接"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="MQTT地址" prop="mqttHost"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mqttHost" placeholder="请填写MQTT地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="MQTT用户" prop="mqttUser"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mqttUser" placeholder="请填写MQTT用户"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="MQTT密码" prop="mqttPwd"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mqttPwd" placeholder="请填写MQTT密码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="NS URL" prop="nsUrl"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nsUrl" placeholder="http://39.103.157.137:8090"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="NS用户名" prop="nsUserName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nsUserName" placeholder="NS用户名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="NS登录密码" prop="nsPassword"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nsPassword" placeholder="NS登录密码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="NS项目ID" prop="nsProjectId"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nsProjectId" placeholder="NS项目ID"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="项目SignToken" prop="nsProjectSignToken"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nsProjectSignToken" placeholder="NS项目SignToken"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="项目SecretKey" prop="nsProjectSecretKey"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nsProjectSecretKey" placeholder="NS项目SecretKey"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="接口SignToken" prop="nsHttpSignToken"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nsHttpSignToken" placeholder="NS项目http SignToken"     ></el-input>
           </el-form-item>
        </el-col>
      <el-col :span="24">
            <el-form-item label="接口SecretKey" prop="nsHttpSecretKey"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nsHttpSecretKey" placeholder="NS项目http SecretKey"     ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
            <el-form-item label="地图链接" prop="mapH5"
                :rules="[
                  {required: true, message:'地图链接不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.mapH5" placeholder="请填写地图链接"     ></el-input>
           </el-form-item>
        </el-col> -->
       <!-- <el-col :span="22">
          <el-form-item label="定位技术" prop="locationType"
            :rules="[
             ]">
            <el-checkbox-group
                v-model="locationTypeList">
               <el-checkbox v-for="item in $dictUtils.getDictList('location_type')" :label="item.label" :key="item.id">{{item.label}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col> -->
        <!-- <el-col :span="24">
            <el-form-item label="MQTT地址" prop="mqttHost"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mqttHost" placeholder="请填写MQTT地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="MQTT用户" prop="mqttUser"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mqttUser" placeholder="请填写MQTT用户"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="MQTT密码" prop="mqttPwd"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mqttPwd" placeholder="请填写MQTT密码"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="24">
            <el-form-item label="项目地址" prop="addr"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.addr" placeholder="请填写地址"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          sort: '',
          addr: '',
          type: '',
          mapH5: '',
          locationType: '',
          mqttHost: '',
          mqttUser: '',
          mqttPwd: '',
          floorHasHeight: '0',
          nsUrl: '',
          nsUserName: '',
          nsPassword: '',
          nsProjectId: '',
          nsProjectSignToken: '',
          nsProjectSecretKey: '',
          nsHttpSignToken: '',
          nsHttpSecretKey: ''
        },
        locationTypeList: []
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建项目`
        } else if (method === 'edit') {
          this.title = '修改项目'
        } else if (method === 'view') {
          this.title = '查看项目'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.inputForm.floorHasHeight = '0';
          this.locationTypeList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/project/jfProject/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfProject)
              if(this.inputForm.locationType && this.inputForm.locationType.length > 1) {
                this.locationTypeList = this.inputForm.locationType.split(",")
              }
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.locationType = this.locationTypeList.join(',')
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/project/jfProject/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
